<?php require_once 'httpFilter.php'; 'conf.php'; ?>
<html>
<title>Transaction view</title>
<head>
	<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/ui-lightness/jquery-ui.css" />
	

<script type="text/javascript"
	src="http://www.google.com/jsapi?key=ABQIAAAAN5ZtdO4QwxMtTu20xDDGaBRDmw7J9fBy0SHOM2DFag_Hy1S5lhQ5rqB1kcjLa1mWGmKBHM2W7xPDyQ">
</script>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script type="text/javascript">
	google.load("jqueryui", "1.8.15");
</script>

<script type="text/javascript" src="js/jquery.datePicker-2.1.2.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="container/jquery.metadata.js"></script>

<link rel="stylesheet" type="text/css" href="css/datePicker.css" title="style"  media="screen"/>

<script type="text/javascript" src="transactionView.js"></script>
<script type="text/javascript" src="js/utils.js">
</script>
</head>
<body>
	<input type="hidden" id="confirmTransInsert" value="undefined"/>
	<input type="hidden" id="currentTransId"/>
	<input type="hidden" id="currentTransType"/>
	<input type="hidden" id="transactionViewSelect" value="<?php echo $_GET["transactionView"]?>"/>
	<input type="hidden" id="accountId" value="<?php echo $_GET["accId"]?>"/>
	<div id="msgDlg"></div>
	 <div id="loadingPic" style="display: none; position:absolute; left: 0px; top:0px; height:100%; width: 100%; text-align: center;
	           background-image:url('images/translucent.gif'); z-index: 1000;">
	<img src="images/loading2.gif" style="margin: 12%"/>
	
	<script language="javascript" type="text/javascript">
		showLoading(true);
	</script>
	
	</div>
	<div id="newCatDlg" style="display: none;">Category name:<br/><input id='newCategory' type='text' class='textInput' value=""/><br/>
						Expense<input type="radio" class="radio" name="catType" value="expense" checked="checked"/>
						Income<input type="radio" class="radio" name="catType" value="income" />
	</div>
	<div id="confirmDlg" title="Confirm"></div>
	
	<div  id="demoContainer" title="Edit Transaction" style="display: none;">
		<table width="450" cellpadding="8">
			<tr>
				<td width="120" nowrap="nowrap">Date:</td>
				<td><input id="transDate" type="text" class="currency" tabindex="2"/></td>
			</tr>
			<tr>
				<td>Description:</td>
				<td><input id="transDescription" type="text" class="longTextInput"/></td>
			</tr>
			<tr>
				<td>Bank Reference:</td>
				<td id="bankRef" nowrap="nowrap"></td>
			</tr>
			<tr>
				<td>Auto Reconcile:</td>
				<td><input id="autoReconcileCB" type="checkbox" onchange="disableAutoRecon();"/>
					<input id="transDescriptor" type="text" class="longTextInput" width="40" disabled value=""/>
				</td>
			</tr>
			<tr>
				<td>Category:</td>
				<td>
					<select id="catSel" onchange="updateDescription();">
						<option id="varExpenses">Variable Expenses</option>
						<option id="fixedExpenses">Fixed Expenses</option>
						<option id="savingsExpenses">Savings Expenses</option> 
						<option id="income">Income</option>
						<option id="transfer">Transfer</option>
					</select> &nbsp;&nbsp; 
					
					<input type="checkbox" id="showAllCategories" onchange="toggleUnusedCategories();"/>Show All
					&nbsp;<img id="transLoadingImg" src="images/ajax-loader.gif"></img>
				</td>
			</tr>
			<tr>
				<td>Type:</td>
				<td>Expense<input type="radio" name="transactionType" value="debit">
					Income<input type="radio" name="transactionType" value="credit"/>
				</td>
			</tr>
			<tr>
				<td>Amount:</td>
				<td>R <input id="transAmount" type="text" class="currency" tabindex="1"/>
				</td>
			</tr>
		</table>
	 </div>
	 
	 <div id="fileUploadContainer"  style="top:100px; left:30%; display: none;"  title="Statement Upload">
	 	<form id="fileUploadForm" name="fileUploadForm" method="post" enctype="multipart/form-data" action="index.php?mod=uploadStatement">
	 		<input type="hidden" id="action" name="action" value="uploadStatement"/>
	 		<input type="hidden" id="accId" name="accId" value="<?php echo $_GET["accId"]?>"/>
			<table width="450">
				<tr>
		 			<td>File Type**:</td>
		 			<td>
		 				CSV <input type="radio" value="CSV" name="uploadStatementType" checked="checked"/>
		 				OFX <input type="radio" value="OFX" name="uploadStatementType" />
	 				</td>
	 			</tr>
	 			<tr id="OFXAvailableAmountTR">
			 			<td>Available Amount:</td><td><input type="text" name="availableAmnt" id="availableAmnt" ><em>same as statement balance if left empty which might not be correct</em></td>
	 			</tr>	
				<tr>
 					<td>Select File:</td>
					<td><input id="transactionFile" type="file" class="longTextInput" name="transactionFile"/></td>
				</tr>
			</table>
		</form>
			<p>**Supported statements: FNB csv or ANY bank's OFX statement file</p>
	</div>	 

	
	<!-- NAVIGATION -->
	<?php include("includes/navigation.php"); ?>
	<?php
			if (isset($_GET["transactionView"])) {
				$transactionView = $_GET["transactionView"];
			} else {
				$transactionView = "first20";
			}
			
			require_once "classes.php";
			$service = new CommonServices;
		   $con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
			if (!$con)
			  {
			  die('Could not connect: ' . mysql_error());
			  }
			
			mysql_select_db(GlobalConfig::db_name, $con);
			$query = "select * from account where id=" . $_GET["accId"];
			error_log($query);
			$result = mysql_query($query);
			$row = mysql_fetch_array($result);
			$openingBalance = $row['openingBalance'];
	?>
	
	<table cellpadding="0" cellspacing="0" summary="August 2010 Budget" width="900" id="theTable">
	<caption>"Getting Rich Slowly"</caption>
	<tr>
	    <th scope="row" colspan="6" class="spec">
	    	<a href="#" onclick="showSearch();"><img src="images/add.png" width="12" border="0"></img></a>
			<?php echo $row["name"] ?>
	    </th>
	    <th align="right" class="spec">
	    <a href="#" id="uploadBtn"><img src="images/upload.png" title="Upload a statement" alt="Upload a statement" border="0" width="15"/></a>
	    </th>
	  </tr>
	  <tr style="display: none;" id="searchPanel" height="40">
	  		<td colspan="4">
	  			Show Transactions:
		    	<select id="transactionView" onchange="updateTransactionView();">
		    		<option value="first20">Last 20</option>
		    		<option value="unreconciled">Unreconciled</option>
		    		<option value="thisMonth">This month</option>
		    		<option value="past3months">Past 3 months</option>
		    		<option value="all">All</option>
		    	</select>
		  		
	  		</td>
	  		<td colspan="3">
	  			Filter: <input id="searchCriteria" type="text" onkeyup="doFilter();"/>
	  		</td>
	  </tr>
	  <tr>
	    <th scope="col" width="100">Date</th>
	    <th scope="col">Description</th>
	    <th scope="col" width="30">Rc</th>
	    <th scope="col" width="80">Expense</th>
	    <th scope="col" width="90">Income</th>
	    <th scope="col" width="90">Balance</th>
	    <th scope="col" width="50" align="center">
	    	<a href="#" id="createTransaction" onclick="createNewTransaction();"><img src="images/new.png" title="Create new transaction" alt="Create new transaction" border="0" width="15"/></a>
		    	&nbsp;&nbsp;
	    	<a href="#" id="deleteBudgetLink" onclick="confirmDeleteTransaction();"><img src="images/delete.png" title="Delete selected transaction" alt="Delete selected transaction" border="0" width="15"/></a>
	    </th>
	  </tr>
	   <?php
	   		$accountId = $_GET["accId"];
	   		$accountBalance = $service->getBalanceForAcccount($accountId);
	   		
			$totalBalance = $accountBalance;

			$query = "select * from transaction where accountId = " . $accountId ." order by date desc limit 20";
			
			if ($transactionView == "all"){
				$query = "select * from transaction where accountId = " . $accountId ." order by date desc";
			} else if ($transactionView == "unreconciled"){
				$query = "select * from transaction where accountId = " . $accountId ." and reconciled is false order by date desc";
			} else if ($transactionView == "past3months"){
				$query = "select * from transaction where accountId = " . $accountId ." and date > DATE_ADD(NOW(), INTERVAL -3 MONTH) order by date desc";
			} else if ($transactionView == "thisMonth"){
				$startDate = $service->getThisMonthStartDate($_SESSION["userid"]);
				$query = "select * from transaction where accountId = " . $accountId ." and date(date) >= '". $startDate ."' and date(date) < NOW() order by date desc";
			}
			
			$result = mysql_query($query);
			error_log($query);
			while($row = mysql_fetch_array($result))
			  {
			 
	 ?>
	  <tr id="<?php echo $row['id']?>" class="rowRow">
	    <td class="spec changeable rowDate"><?php echo$row['date'] ?></td>
	    
	    <? if ( trim($row['description']) != ""){  ?>
	   	 	<td class="spec changeable rowDescription" ><a href="#" onclick="openContainer(<?php echo $row['id']?>); return false;"><?php echo str_replace("\\", "",$row['description']) ?></a></td>
	    <?} else { ?>
	    	<td class="spec changeable rowDescription" ><a href="#" onclick="openContainer(<?php echo $row['id']?>); return false;"><?php echo (trim($row['bankRef']) == "" ? "UNSPECIFIED" : $row['bankRef'])  ?></a></td>
	    <? } ?>
	    
	    <? if ($row['reconciled'] == 1 ){ ?>
	    <td><input disabled type="checkbox" class="checkbox" name="reconciled" value="reconciled" checked="true"/></td>
	    <? } else { ?>
	     <td><input disabled enabled="false" type="checkbox" class="checkbox" name="reconciled" value="reconciled"/></td>
	     <? } ?>
	     
	     
	    <? if ($row['amount'] < 0 ){ ?>
	    <td class="spec changeable rowAmount" onclick="changeToInput(this);">R <?php echo ($row['amount'] * -1) ?></td>
	    <td class="spec">&nbsp;</td>
	    
	    <? } else { ?>
	    	
    	<td class="spec">&nbsp;</td>
	    <td class="spec changeable rowAmount" onclick="changeToInput(this);">R <?php echo$row['amount'] ?></td>
	    
	    <? } ?>
	   
	    <td class="spec runningBalance">R <?php echo$totalBalance ?></td>
	    <td><input type="radio" name="selectTransaction" value="<?php echo $row['id'] ?>"></td>
	  </tr>
	 
	   
	  <?php
	  	
	  $totalBalance =  $totalBalance - $row['amount'];
	  	}
	  	
	  	$query = "select * from account where id = " . $accountId;
	  	error_log($query);
	  	$result = mysql_query($query);
			
		$row = mysql_fetch_array($result);
	  ?>
	  
	   <tr>
	    <th scope="row" class="nobg" colspan="5">&nbsp;</th>
	    <td id="totalAmount" class="total" nowrap="nowrap"><?php echo $accountBalance ?></td>
	    <th class="nobg">&nbsp;</th>
	   </tr>
	 </table>
	 
	  <?php
		mysql_close($con);
	 ?>
	 
	 <script language="javascript" type="text/javascript">
	 	var dupMap = new Object();
	 	$("#theTable").find("tr.rowRow").each(function(count,element){
	 		var theDate = $(this).find(".rowDate").html();
	 		var theDescription = $(this).find(".rowDescription").find("a").html();
	 		var theAmount = $(this).find(".rowAmount").html();
	 		//alert("dup: " + theDate+theDescription+theAmount);
	 		if(typeof(dupMap[theDate+theDescription+theAmount])!="undefined"){
	 			var location = dupMap[theDate+theDescription+theAmount];
	 			$(this).find("td").addClass("unreconciled");
	 			$($("#theTable").find("tr.rowRow").get(location)).find("td").addClass("unreconciled");
	 		}
	 		dupMap[theDate+theDescription+theAmount] = count;
	 	});
	 	showLoading(false);
	 </script>

<?php include("includes/footer.php"); ?>
</body>
</html>
